//
//  ComposeToolBar.m
//  CMJWeibo
//
//  Created by BH on 16/1/7.
//
//

#import "ComposeToolBar.h"
#import "UIImage+MJ.h"
#import "UIView+MJ.h"

@implementation ComposeToolBar

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithName:@"compose_toolbar_background"]];
        
        //添加按钮
        [self addBtnWithIcon:@"compose_camerabutton_background" highIcon:@"compose_camerabutton_background_highlighted" tag:ComposeToolBarButtonTypeCamera];
        [self addBtnWithIcon:@"compose_toolbar_picture" highIcon:@"compose_toolbar_picture_highlighted" tag:ComposeToolBarButtonTypePicture];
        [self addBtnWithIcon:@"compose_mentionbutton_background" highIcon:@"compose_mentionbutton_background_highlighted" tag:ComposeToolBarButtonTypeMention];
        [self addBtnWithIcon:@"compose_trendbutton_background" highIcon:@"compose_trendbutton_background_highlighted" tag:ComposeToolBarButtonTypeTrend];
        [self addBtnWithIcon:@"compose_emoticonbutton_background" highIcon:@"compose_emoticonbutton_background_highlighted" tag:ComposeToolBarButtonTypeEmotion];
    }
    return self;
}

-(void)addBtnWithIcon:(NSString *)icon highIcon:(NSString *)highIcon tag:(ComposeToolBarButtonType)tag
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.tag = tag;
    [button setImage:[UIImage imageWithName:icon] forState:UIControlStateNormal];
    [button setImage:[UIImage imageWithName:highIcon] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
}

-(void)buttonClick:(UIButton *)button
{
    if ([self.delegate respondsToSelector:@selector(composetoolbar:didClickButtonType:)]) {
        [self.delegate composetoolbar:self didClickButtonType:button.tag];
    }
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat buttonW = self.width/self.subviews.count;
    CGFloat buttonH = self.height;
    for (int i=0; i<self.subviews.count; i++) {
        UIButton *button = self.subviews[i];
        button.x = buttonW*i;
        button.frame = CGRectMake(button.x, 0, buttonW, buttonH);
    }
}

@end
